// pages/search/index.js
import { request } from "../../utils/request.js";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isShow: false,
    itemList: [],
    isValue: "",
  },
  Timer: -1,
  // 搜索框输入触发
  handleInput(e) {
    let { value } = e.detail;
    // console.log(value);
    // 判断关键字的合理性
    if (!value.trim()) {
      // 不合理
      clearInterval(this.Timer);
      this.Timer = setTimeout(() => {
        this.setData({
          isShow: false,
          itemList: [],
          isValue: "",
        });
      }, 500);
      return;
    }
    this.setData({
      isShow: true,
      isValue: value,
    });
    clearInterval(this.Timer);
    this.Timer = setTimeout(() => {
      this.getGoodsRequset(value);
    }, 1000);
  },
  // 切换按钮显示状态
  hadleChangeShow() {
    this.setData({
      isValue: "",
      itemList: [],
      isShow: false,
    });
  },
  // 根据输入框的value请求获取数据
  async getGoodsRequset(value) {
    let res = await request("/goods/qsearch", { query: value });
    //  console.log(res);
    this.setData({
      itemList: res,
    });
  },
});
